﻿#include "qtmain.h"

#include "includeBase.h"

#include "pcl/providers/PclDefaultProvider.h"
#include "widgets/PointCloudWidget.h"
#include "widgets/PropertyEntityProvider.h"
#include "widgets/PropertyManager.h"
#include "widgets/PropertyPaneWidget.h"

#include "core/AppSignals.h"
#include "pcl/PclContext.h"
#include "utils/GuiBuilder.h"

namespace {
GuiBuilder builder;
}

qtMain::qtMain(QWidget* parent)
    : QMainWindow(parent)
{
    this->setupUi();
}

qtMain::~qtMain() {}

void qtMain::setupUi()
{
    setupApp();

    // 避免窗口不能正常最大化
    setMinimumSize(QSize(1024, 768));
    renderTitle();
    this->setWindowIcon(QIcon(":/icons/app.svg"));
    this->setAutoFillBackground(true);

    builder.setMainWindow(this);
    setupMenuBar();
    setupToolBar();
    setupStatusBar();
    setupPropertyPane();
    setupCentralWidget();
    setupSignals();
}

void qtMain::setupApp()
{
    AppContext::setMainWindow(this);
    ActionManager::get().init();
}

void qtMain::setupMenuBar()
{
    builder.setMenuBar(this->menuBar())
        .addMenu("文件(&F)")
        .addMenuAction(ActionManager::get().fileNewAction)
        .addMenuAction(ActionManager::get().fileOpenAction)
        .addMenuAction(ActionManager::get().fileSaveAction)
        .addMenuAction(ActionManager::get().fileSaveAsAction)
        .addMenuSeparator()
        .addMenuAction(ActionManager::get().filePrintAction)
        .addMenuAction(ActionManager::get().filePreviewAction)
        .addMenuSeparator()
        .addMenuAction(ActionManager::get().fileExportCsvAction)
        .addMenuSeparator()
        .addMenuAction(ActionManager::get().fileLoadSettingsAction)
        .addMenuSeparator()
        .addMenuAction(ActionManager::get().fileExitAction)
        .addMenu("点云")
        .addMenuAction(ActionManager::get().pclDefaultAction)
        .addMenuAction(ActionManager::get().pclAddLineAction)
        .addMenuAction(ActionManager::get().pclSurfacesAction)
        .addMenu("功能")
        .addMenuAction(ActionManager::get().imageTabAction)
        .addMenu("帮助(&H)")
        .addMenuAction(ActionManager::get().helpAboutAction);
}

void qtMain::setupToolBar()
{
    QToolBar* toolbar = new QToolBar(this);
    this->addToolBar(toolbar);
    toolbar->setIconSize(QSize(48, 48));
    toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    // toolbar->setToolButtonStyle(Qt::ToolButtonIconOnly);

    builder.setToolBar(toolbar)
        .addToolBarAction(ActionManager::get().fileOpenAction)
        .addToolBarAction(ActionManager::get().fileSaveAction)
        .addToolBarAction(ActionManager::get().fileSaveAsAction)
        .addToolBarSeparator()
        .addToolBarAction(ActionManager::get().filePrintAction)
        .addToolBarAction(ActionManager::get().filePreviewAction)
        .addToolBarSeparator()
        .addToolBarAction(ActionManager::get().fileExportCsvAction)
        .addToolBarSeparator()
        .addToolBarAction(ActionManager::get().imageTabAction);
}

void qtMain::setupStatusBar()
{
    // 状态栏
    MainStatusBarManager::create(this->statusBar());

    // 连接信号槽
    connect(
        appSignals, &AppSignals::onStatusMessage, this, &qtMain::onStatusMessage
    );
}

void qtMain::setupPropertyPane()
{
    PropertyPaneWidget* propertyPane = new PropertyPaneWidget(this);
    builder.addDock(propertyPane, "项目属性", Qt::LeftDockWidgetArea);
    onProjectLoaded();
}

void qtMain::setupCentralWidget()
{
    // QMdiArea* mdiArea_ = new QMdiArea(this);
    // this->setCentralWidget(mdiArea_);
    // mdiArea_->setViewMode(QMdiArea::TabbedView);
    // mdiArea_->setTabsClosable(true);
    // mdiArea_->setTabsMovable(true);

    tabCenter = new QTabWidget(this);
    tabCenter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    this->setCentralWidget(tabCenter);
    viewReport = new ReportWidget();
    builder.setGridLayout(this->centralWidget())
        .setTabWidget(tabCenter)
        .addTab(new PointCloudWidget(), "点云")
        .addTab(new SelectBoxWidget(), "图像页")
        .addTab(viewReport, "报表");
    tabCenter->setCurrentIndex(0);
    onTabBarClicked(0);
}

void qtMain::setupSignals()
{
    connect(
        appSignals, &AppSignals::onProjectLoaded, this, &qtMain::onProjectLoaded
    );
    connect(
        appSignals, &AppSignals::onProjectSaved, this, &qtMain::onProjectSaved
    );
    connect(
        appSignals, &AppSignals::onShowImageTab, this, &qtMain::onShowImageTab
    );
    connect(
        tabCenter, &QTabWidget::tabBarClicked, this, &qtMain::onTabBarClicked
    );
}

void qtMain::renderTitle()
{
    this->setWindowTitle(QString("%1 - %2")
                             .arg(AppContext::ProductName())
                             .arg(AppContext::projectDocumentStatus()->fileName)
    );
}

void qtMain::closeEvent(QCloseEvent* e)
{
    AppContext::close();
    PclContext::get().close();
}

void qtMain::onProjectLoaded()
{
    renderTitle();
}

void qtMain::onProjectSaved()
{
    renderTitle();
}

void qtMain::onTabBarClicked(int index)
{
    if (index == 0) {
        // 设置属性
        shared_ptr<PclDefaultProvider> provider =
            make_shared<PclDefaultProvider>();
        PropertyManager::get().setCurrent(provider);
    } else if (index == 1) {
        // 设置属性
        shared_ptr<PropertyEntityProvider> provider =
            make_shared<PropertyEntityProvider>();
        provider->setEntity(AppContext::projectDocument());
        PropertyManager::get().setCurrent(provider);
    } else if (index == 2) {
        viewReport->loadReport();
    }
}

void qtMain::onStatusMessage(const QString& mesage, int timeout)
{
    MainStatusBarManager::get()->showMessage(mesage, timeout);
}

void qtMain::onShowImageTab()
{
    tabCenter->setCurrentIndex(0);
}
